package entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;

@SuppressWarnings("serial")
@Entity
@Table(name = "Utente")
public class Utente implements Serializable {

	private String username;
	private byte[] password;
	private String nome;
	private String cognome;
	private byte[] fotoProfilo;
	
	private boolean isAdmin;

	
	private List<Abilita> abilita=new ArrayList<Abilita>();;

	public Utente() {
		super();
	}

	public Utente(String username, byte[] password, String nome,
			String cognome, byte[] fotoProfilo, boolean isAdminU) {
		super();
		
		setUsername(username);
		setPassword(password);
		setNome(nome);
		setCognome(cognome);
		setFotoProfilo(fotoProfilo);
		setAdmin(isAdminU);
	}

		@Id
	@Column(name = "username", unique = true)
	public String getUsername() {
		return username;
	}

	public void setUsername(String usernameU) {
		this.username = usernameU;
	}

	@Column(name = "password")
	public byte[] getPassword() {
		return password;
	}

	public void setPassword(byte[] password) {
		this.password = password;
	}

	@Column(name = "nome")
	public String getNome() {
		return nome;
	}

	private void setNome(String nome) {
		this.nome = nome;
	}

	@Column(name = "cognome")
	public String getCognome() {
		return cognome;
	}

	private void setCognome(String cognome) {
		this.cognome = cognome;
	}

	@Lob
	@Basic(fetch = FetchType.LAZY)
	@Column(length = 100000, name = "fotoProfilo")
	// fino a 1Mb
	public byte[] getFotoProfilo() {
		return fotoProfilo;
	}

	private void setFotoProfilo(byte[] fotoProfilo) {
		this.fotoProfilo = fotoProfilo;
	}

	@Column(name = "isAdmin")
	public boolean isAdmin() {
		return isAdmin;
	}

	public void setAdmin(boolean isAdm) {
		this.isAdmin = isAdm;
	}

	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "UtenteAbilita", joinColumns = { @JoinColumn(name = "username", nullable = false) }, inverseJoinColumns = { @JoinColumn(name = "idAbilita", nullable = false) })
	
	public List<Abilita> getAbilita() {
		return abilita;
	}
	
	public void setAbilita(List<Abilita> abilita) {
		this.abilita = abilita;
	}
public void setNewAbilita(Abilita ability)
{
	abilita.add(ability);
}

public void rimuoviAbilita(Abilita abilita)
{
	this.abilita.remove(abilita);
}

public void svuotaAbilita()
{
	int i;
	for(i=0;i<abilita.size();i++)
	{
		abilita.remove(i);
	}
}

}